package apps.AfterDlcs;

import apps.AfterDlcs.AfterDlcs;
import apps.AfterDlcs.afterDlcsBL.Keys;
import apps.AfterDlcs.afterDlcsBL.city.City;
import apps.AfterDlcs.afterDlcsBL.hero.HeroBL;
import apps.AfterDlcs.afterDlcsBL.hero.HeroKeys;
import backs.app.AppMenu;
import backs.bean.Message;
import backs.dataBase.DBNormal;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AfterDlcsHeroListMenu extends AppMenu
{
    Map<String,Integer> heroNameMap=new HashMap<>();
    String name,showOut,shownInside;

    public AfterDlcsHeroListMenu(String name, String showOut, String shownInside,AppMenu appMenu)
    {
        super(name,showOut,appMenu);
        this.name=name;
        this.showOut=showOut;
        this.shownInside=shownInside;
    }

    @Override
    public void handleMessage(Message message)
    {
        if(message.message.startsWith(name))
        {
            initHero(message);
            message.message=message.message.substring(name.length());
            if(heroNameMap.containsKey(message.message))
            {
                handleHero(message.getPrivateAsGroupID(),heroNameMap.get(message.message),message);
                return;
            }
            else
            {
                for(String heroNa:heroNameMap.keySet())
                {
                    if(message.message.startsWith(heroNa))
                    {
                        message.message=message.message.substring(heroNa.length());
                        handleHeroStart(message.getPrivateAsGroupID(),heroNameMap.get(heroNa),message);
                        return;
                    }
                }

            }
            String out="---"+name+"---\n"+shownInside+"\n";
            for(String herNa:heroNameMap.keySet())out+=this.fathersName+name+herNa+"\n";
            message.sendBack(out);
        }

    }

    public void initHero(Message message)
    {
        List<String> heros = DBNormal.getValues(AfterDlcs.dbName, message.getPrivateAsGroupID(), message.userID + "_heroList");
        for (String heroID : heros) {
            String heroName = HeroBL.getHeroValue(message.getPrivateAsGroupID(), Integer.parseInt(heroID), HeroKeys.NAME, heroID, false);
            String alive=HeroBL.getHeroValue(message.getPrivateAsGroupID(), Integer.parseInt(heroID), HeroKeys.ALIVE, "1", false);
            int locationX=Integer.parseInt(HeroBL.getHeroValue(message.getPrivateAsGroupID(), Integer.parseInt(heroID), Keys.LOCATION_X, "0", false));
            int locationY=Integer.parseInt(HeroBL.getHeroValue(message.getPrivateAsGroupID(), Integer.parseInt(heroID), Keys.LOCATION_Y, "0", false));
            City city= City.getCity(message.getPrivateAsGroupID(),message.userID);
            if("1".equals(alive)&&locationX==city.getLocationX()&&locationY==city.getLocationY())
                heroNameMap.put("("+heroName+")",Integer.parseInt(heroID));
        }
    }

    public void handleHero(int worldID,int heroID,Message message)
    {

    }

    public void handleHeroStart(int worldID,int heroID,Message message)
    {

    }
}
